package com.heima.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.apis.wemedia.IWmUserClient;
import com.heima.common.identification.UserConstants;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.dtos.AuthDto;
import com.heima.model.user.pojos.ApUser;
import com.heima.model.user.pojos.ApUserRealname;
import com.heima.user.mapper.ApUserMapper;
import com.heima.user.mapper.ApUserRealnameMapper;
import com.heima.user.service.ApUserRealnameService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;

@Service
@Slf4j
@Transactional//加入事务
public class ApUserRealnameServiceImpl extends ServiceImpl<ApUserRealnameMapper, ApUserRealname> implements ApUserRealnameService {

    @Autowired
    private ApUserRealnameMapper apUserRealnameMapper;

    @Override
    public ResponseResult loadListByStatus(AuthDto dto) {
        if (dto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //2
        Integer page = dto.getPage();
        Integer size = dto.getSize();
        page = page == null ? 1 : page;
        size = size == null ? 10 : size;
        //3
        //2-3 可以替换为下面代码
//        dto.checkParam();
        //3
//        IPage page1 = new Page(dto.getPage(), dto.getSize());
//        LambdaQueryWrapper<ApUserRealname> lambdaQueryWrapper = new LambdaQueryWrapper();
//        if(dto.getStatus() != null){
//            lambdaQueryWrapper.eq(ApUserRealname::getStatus,dto.getStatus());
//        }
//        page1 = page(page1,lambdaQueryWrapper);
        //4
        //3-4可以替换到 54行代码
        Page<ApUserRealname> pageInfo = new Page<>(page, size);
        LambdaQueryWrapper<ApUserRealname> wrapper = new LambdaQueryWrapper<>();
        if (dto.getStatus() != null) {
            wrapper.eq(ApUserRealname::getStatus, dto.getStatus());
        }
        Page<ApUserRealname> pageResult = apUserRealnameMapper.selectPage(pageInfo, wrapper);

        Long total = pageResult.getTotal();

        //返回结果
        PageResponseResult responseResult = new PageResponseResult(page, size, total.intValue());
        responseResult.setData(pageResult.getRecords());

        return responseResult;
    }

    @Autowired
    private IWmUserClient iWmUserClient;

    @Autowired
    private ApUserMapper apUserMapper;

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public ResponseResult updateStatus(AuthDto dto, Short status) {
        // 检查参数
        if (dto == null || dto.getId() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        ApUserRealname apUserRealname = apUserRealnameMapper.selectById(dto.getId());
        if (apUserRealname == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }


        // 修改认证状态
        ApUserRealname apUserRealnameAfter = new ApUserRealname();
        apUserRealnameAfter.setId(dto.getId());
        apUserRealnameAfter.setStatus((short) (status == 9 ? 9 : 2));
        if (StringUtils.isNotBlank(dto.getMsg())) {
            apUserRealnameAfter.setReason(dto.getMsg());
        }
        apUserRealnameMapper.updateById(apUserRealnameAfter);


        //如果审核状态是9 就是成功 需要创建自媒体账户
        if (status.equals(UserConstants.PASS_AUTH)) {
            HashMap<String, String> map = new HashMap<>();
            String name = apUserRealname.getName();
            map.put("name", name);
            ResponseResult responseResult = iWmUserClient.addUser(map);
            if (responseResult == null || responseResult.getCode() != 200) {
                throw new RuntimeException("自媒体用户创建失败");
            }
            ApUser apUser = new ApUser();
            apUser.setId(apUserRealname.getUserId());
            apUser.setFlag((short) 1);
            int updateResult = apUserMapper.updateById(apUser);
            if (updateResult < 1) {
                throw new RuntimeException("APP用户状态更新失败");
            }

        }
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }


}
